<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:body>
        <p:panel id="search" header="Search">
            <h:form id="selectArticleStateForm">
                <p:panelGrid columns="2" styleClass="organisationGrid">
                    <h:outputText value="Categories" styleClass="outputTextLabel"/>
                    <p:selectManyCheckbox value="#{searchArticle.selectedCategories}">
                        <f:selectItems value="#{submitArticleBean.categoriesValue}"/>
                    </p:selectManyCheckbox>
                    <h:outputText value="Name" styleClass="outputTextLabel"/>
                    <h:inputText value="#{searchArticle.keyWord}"/>
                    <p:commandButton value="Search" action="#{searchArticle.search}" 
                                     icon="ui-icon-search" update=":result,:headerForm:growl"/>
                </p:panelGrid>
            </h:form>
        </p:panel>

        <p:panel id="result" header="Result">
            <p:panelGrid columns="2" styleClass="organisationGrid">
                <p:dataTable id="dataArticle" var="article" value="#{searchArticle.searchResult}"
                             styleClass="standardDataTable" paginator="true" rows="10">
                    <p:column headerText="Name">
                        <h:outputText value="#{article.name}"/>
                    </p:column>
                    <p:column headerText="Auction's End">
                        <h:outputText value="#{article.endSale}">
                            <f:convertDateTime pattern="dd/MM/yyyy HH:mm" />
                        </h:outputText>
                    </p:column>
                    <p:column headerText="Last Auction">
                        <h:outputText rendered="#{article.actualPrice != 0}"  styleClass="outputTextValue" 
                                      value="#{article.actualPrice}">
                            <f:convertNumber type="currency"/>
                        </h:outputText>
                        <h:outputText rendered="#{article.actualPrice == 0}"  styleClass="outputTextValue" 
                                      value="#{article.startPrice}">        
                            <f:convertNumber type="currency"/>
                        </h:outputText>
                    </p:column>
                    <p:column headerText="Details"> 
                        <h:form id="viewDetailsForm">
                            <p:commandLink id="selectButton" update=":displayArticle" action="#{searchArticle.articleSelected()}" >  
                                <img src="resources/images/ic_show.png" />
                                <f:setPropertyActionListener value="#{article}" target="#{searchArticle.selectedArticle}" /> 
                            </p:commandLink>
                        </h:form>
                    </p:column>
                </p:dataTable>  
                <p:panel id="displayArticle" header="Details" >
                    <h:form>
                        <p:panelGrid styleClass="organisationGrid" columns="2">
                            <h:outputText value="Name" styleClass="outputTextLabel"/>
                            <h:outputText  styleClass="outputTextValue" value="#{searchArticle.selectedArticle.name}"/>
                            <h:outputText value="Description" styleClass="outputTextLabel"/>
                            <h:outputText  styleClass="outputTextValue" value="#{searchArticle.selectedArticle.description}"/>
                            <h:outputText value="Last Auction" styleClass="outputTextLabel"/>
                            <h:outputText rendered="#{searchArticle.selectedArticle.actualPrice != 0}"  styleClass="outputTextValue" 
                                          value="#{searchArticle.selectedArticle.actualPrice}">
                                <f:convertNumber type="currency"/>
                            </h:outputText>
                            <h:outputText rendered="#{searchArticle.selectedArticle.actualPrice == 0}"  styleClass="outputTextValue" 
                                          value="#{searchArticle.selectedArticle.startPrice}">        
                                <f:convertNumber type="currency"/>
                            </h:outputText>
                            <h:outputText value="Seller" styleClass="outputTextLabel"/>
                            <h:outputText  styleClass="outputTextValue" 
                                           value="#{searchArticle.selectedArticle.seller.firstName} #{searchArticle.selectedArticle.seller.lastName}"/>
                            <h:outputText value="Reputation" styleClass="outputTextLabel"/>
                            <h:outputText  styleClass="outputTextValue" 
                                           value="#{searchArticle.selectedArticle.seller.reputation}"/>
                        </p:panelGrid>
                        <p:panelGrid columns="4" styleClass="organisationGrid">
                            <h:outputText value="New Bid" styleClass="outputTextLabel"/>
                            <p:spinner value="#{searchArticle.newBid}" required="true" id="bidInput" label="Bid" 
                                       min="#{(searchArticle.selectedArticle.actualPrice + 1)}">
                            </p:spinner>
                            <h:outputText value="€"/>
                            <p:commandButton value="confirmBid" oncomplete="confirmBid.show()" icon="ui-icon-cart" update=":confirmForm"
                                             disabled="#{(empty(searchArticle.selectedArticle) || (searchArticle.selectedArticle.seller.id == headerBean.loggedUser.id) || (searchArticle.selectedArticle.winningBid.bidder.id == headerBean.loggedUser.id))}" />
                        </p:panelGrid>
                    </h:form>
                </p:panel>
            </p:panelGrid>
        </p:panel>

        <h:form id="confirmForm">
            <p:confirmDialog id="confirmBid" message="Do you confirm bid on the item #{searchArticle.selectedArticle.name} worth € #{searchArticle.newBid}  ?"  
                             header="Bid confirmation" severity="message" widgetVar="confirmBid">
                <p:commandButton id="confirm" value="Yes Sure"  oncomplete="confirmBid.hide()"
                                 actionListener="#{searchArticle.createNewBid}" update=":result"/>
                <p:commandButton id="decline" value="Not Yet" onclick="confirmBid.hide()" type="button" />   
            </p:confirmDialog>
        </h:form>        
    </h:body>
</html>